<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <myHead th:replace="basic/layout :: head('用户管理')"></myHead>
    <!-- Bootstrap 5 -->
</head>
<body>
<div class="container">
    <div th:replace="~{basic/layout :: topNav('back', ${currentUser})}"></div>
    <div class="row" style="width: 100%;margin: -10px 0;">
        <div th:replace="basic/layout :: menu"></div>

        <div class="col-10">
            <div class="container mt-4">
                <!-- 标题与操作按钮 -->
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2>用户管理</h2>
                    <!-- 新增按钮（仅admin:edit权限可见） -->
                    <a th:href="@{/roles/add}" class="btn btn-primary"
                       sec:authorize="hasAuthority('admin:edit')">
                        <i class="bi bi-plus-circle"></i> 新增用户
                    </a>
                </div>

                <!-- 消息提示 -->
                <div th:if="${msg}" class="alert alert-success alert-dismissible fade show">
                    <span th:text="${msg}"></span>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
                <div th:if="${error}" class="alert alert-danger alert-dismissible fade show">
                    <span th:text="${error}"></span>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>

                <!-- 用户表格 -->
                <div class="card shadow-sm">
                    <div class="card-body">
                        <table class="table table-hover">
                            <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>用户名</th>
                                <th>昵称</th>
                                <th>邮箱</th>
                                <th>手机号</th>
                                <th>状态</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr th:each="user : ${users}">
                                <td th:text="${user.id}"></td>
                                <td th:text="${user.username}"></td>
                                <td th:text="${user.nickname}"></td>
                                <td th:text="${user.email} ?: '-'"></td>
                                <td th:text="${user.phone} ?: '-'"></td>
                                <td>
                                    <span class="badge bg-success" th:if="${user.status == 1}">正常</span>
                                    <span class="badge bg-danger" th:if="${user.status == 0}">禁用</span>
                                </td>
                                <!--                            使用的Date，所以要用dates-->
                                <td th:text="${#dates.format(user.createTime, 'yyyy-MM-dd HH:mm')}"></td>
                                <td>
                                    <!-- 编辑按钮（仅admin:edit权限可见） -->
                                    <a th:href="@{/roles/edit/{id}(id=${user.id})}"
                                       class="btn btn-sm btn-outline-primary me-2 btn-primary"
                                       sec:authorize="hasAuthority('admin:edit')">
                                        编辑
                                    </a>
                                    <!-- 删除按钮（仅admin:delete权限可见） -->
                                    <a th:href="@{/roles/delete/{id}(id=${user.id})}"
                                       class="btn btn-sm btn-outline-danger btn-danger"
                                       sec:authorize="hasAuthority('admin:delete')"
                                       onclick="return confirm('确定删除该用户吗？')">
                                        删除
                                    </a>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>